package control.util
{
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.utils.Timer;
	
	import org.osmf.events.TimeEvent;

	/**
	 * 游戏中用到的工具 
	 * @author leo
	 * 
	 */	
	public class GameUtil
	{
		private static var _instance :GameUtil = null;
		/**
		 * 时间器 
		 */		
		private var timer :Timer;
		/**
		 * 超时设置 
		 */		
		private var totalTime :int = 60000;
		/**
		 * 当前使用时间 
		 */		
		private var curTime :int;
		/**
		 * 随机数组 
		 */		
		private var randomArr:Array;
		public function GameUtil()
		{
		}

		/**
		 * 反复检查实例中的属性是否为真（其实就是等加载完成）
		 * @param obj
		 * @param value
		 * @param cb
		 * @param delay
		 * 
		 */		
		public function checkIsTrue(obj :*,value :String,cb :Function,delay :int = 30):void
		{
			this.curTime = 0;
			if(this.timer)
			{
				this.timer.reset();
				this.timer.removeEventListener(TimerEvent.TIMER,timerHandler);
				this.timer.stop();
				this.timer = null;
			}
			this.timer = new Timer(delay);
			this.timer.addEventListener(TimerEvent.TIMER,timerHandler);
			this.timer.start();
			
			function timerHandler(event :TimerEvent):void
			{
				if(obj[value])
				{
					timer.removeEventListener(TimerEvent.TIMER,timerHandler);
					timer.reset();
					timer.stop();
					timer = null;
					if(cb != null)
					{
						cb();
						cb = null;
					}
				}else
				{
					this.curTime += delay;
					//超过60秒没反应就退出
					if(this.curTime >= this.totalTime)
					{
						this.timer.reset();
						this.timer.stop();
						this.timer.removeEventListener(TimerEvent.TIMER,timerHandler);
						this.curTime = 0;
						trace("加载小地图超时退出");
					}
				}
			}
			
		}
		/**
		 * 设置随机范围 
		 * @param min
		 * @param max
		 * 
		 */		
		public function setRandom(min:uint = 1, max:uint = 1000):void
		{
			randomArr=new Array;
			for (var i:uint=min; i<= max; i++) 
			{
				randomArr.push(i);
			}
		}
		/**
		 * 获取不重复的随机值 
		 * @return 
		 * 
		 */		
		public function getRandom():int 
		{
			var num:int;
			var temp:int;
			num=int(randomArr.length*Math.random());
			
			temp = randomArr[num];
			//trace(temp);
			randomArr.splice(num,1);
			
			return temp;
		}
		/**
		 * 判断是否为空 
		 * @return 
		 * 
		 */		
		public function isNull():Boolean 
		{
			if(randomArr == null || randomArr.length == 0)
			{
				return true;
			}
			return false;
		}
		
		public static function get instance():GameUtil
		{
			if(_instance == null)
			{
				_instance = new GameUtil();
			}
			return _instance;
		}

	}
}